package figura;

import figura.personajes.Player;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferStrategy;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author admin
 */
public class Pantalla extends javax.swing.JFrame implements KeyListener {

    static Player player;
    public BufferStrategy strategy;
    public boolean[] key = new boolean[256];

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        Pantalla pantalla = new Pantalla();

        player = new Player();
        player.carga_frames();
        pantalla.actualizar();
    }

    /** Creates new form Pantalla */
    public Pantalla() {
        initComponents();
        // Pantalla pantalla = new Pantalla();
        this.setBounds(0, 0, 600, 480);
        EscuchadorDeTeclado escuchadorTeclado = new EscuchadorDeTeclado();
        escuchadorTeclado.start();
        addKeyListener(this);
        createBufferStrategy(2);
        strategy = getBufferStrategy();

        this.setVisible(true);
    //escuchadorTeclado.continuar=false;

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setBackground(new java.awt.Color(0, 0, 0));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    public void actualizar() {
        while (true) {
            this.pintaMundo();
            try {
                Thread.sleep(80);
            } catch (InterruptedException ex) {
                Logger.getLogger(Pantalla.class.getName()).log(Level.SEVERE, null, ex);
            }

        }
    }

    public void pintaMundo() {
        Graphics g = strategy.getDrawGraphics();
        g.setColor(Color.BLACK);
        g.fillRect(0, 0, 1024, 600);

        if (player.isDesacelerandose()) {
            player.desaceleracion();
        }

        if (player.isEstaSaltando()) {

            if (player.direccion_movimiento == 0) {
                player.direccion_movimiento = 4;
                player.bufferMovimiento = 0;
            }
            if (player.direccion_movimiento == 1) {
                player.direccion_movimiento = 5;
                player.bufferMovimiento = 1;
            }

            player.saltar();
        }

        if (player.isEstaCayendo()) {
            player.caer();
        }

        player.paint(g);
        strategy.show();

    }

    public void keyTyped(KeyEvent e) {
    }

    public void keyPressed(KeyEvent e) {
        key[e.getKeyCode()] = true;

    }

    public void keyReleased(KeyEvent e) {
        key[e.getKeyCode()] = false;

    }

    private void actualizar_teclado() {
        if (!key[KeyEvent.VK_LEFT] && !key[KeyEvent.VK_RIGHT]) {
            player.setDesacelerandose(true);
        }

        if (key[KeyEvent.VK_LEFT]) {  //izquierda
            if (player.direccion_movimiento == 1) {
                key[KeyEvent.VK_LEFT] = false;
                key[KeyEvent.VK_RIGHT] = false;
                player.direccion_movimiento = 1;
            }
            if ((player.direccion_movimiento == -1 || player.direccion_movimiento == 0)) {
                player.direccion_movimiento = 0;
                player.bufferMovimiento = 0;
                player.aceleracion();
                player.mueve_izquierda();

            }
        }
        if (key[KeyEvent.VK_RIGHT]) {
            if (player.direccion_movimiento == 0) {
                key[KeyEvent.VK_LEFT] = false;
                key[KeyEvent.VK_RIGHT] = false;
                player.direccion_movimiento = 0;
            }
            if ((player.direccion_movimiento == -1 || player.direccion_movimiento == 1)) {
                player.direccion_movimiento = 1;
                player.bufferMovimiento = 1;
                player.aceleracion();
                player.mueve_derecha();
            }
        }

        if (key[KeyEvent.VK_UP] && !player.isEstaSaltando() && !player.isEstaCayendo()) {
            player.setEstaSaltando(true);
        }

        if (key[KeyEvent.VK_DOWN] && player.direccion_movimiento == -1) {
            if (!player.isAgachado()) {
                //player.agacharse();
                player.setAgachado(true);
            }
        }
        if (!key[KeyEvent.VK_DOWN]) {
            player.setAgachado(false);
        }


    }

    public class EscuchadorDeTeclado extends Thread {

        public boolean continuar = true;

        @Override
        public void run() {
            while (continuar) {

                try {
                    sleep(50);
                } catch (InterruptedException ex) {
                    Logger.getLogger(Pantalla.class.getName()).log(Level.SEVERE, null, ex);
                }
                actualizar_teclado();
            }
        }
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables
}
